<template>
  <div v-if="open" class="modal">
    <div v-click-outside="clickOutsideHandler" class="modal-content">
      <div class="list">
        <div v-for="coin in Object.keys(coinInfo)" :key="coin" class="item" @click="handleClick(coin)">
          <div class="item-coin">
            <div class="coin-icon">
              <img :src="coinInfo[coin].icon" alt="">
            </div>
            <div class="coin-name">{{ coinInfo[coin].coin_name }}</div>
          </div>
          <div v-if="money === coin" class="item-selector"><i class="iconfont icon-duigou"></i></div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex'
export default {
  data () {
    return {
      open: false
    }
  },
  computed: {
    ...mapState({
      money: ({ otc }) => otc.money,
      coinInfo: ({ otc }) => otc.coinInfo
    })
  },
  methods: {
    ...mapActions({
      setMoney: 'otc/setMoney'
    }),
    show () {
      this.open = true
    },
    hide () {
      this.open = false
    },
    clickOutsideHandler () {
      if (this.open) {
        this.hide()
      }
    },
    handleClick (money) {
      this.setMoney(money)
      this.hide()
    }
  }
}
</script>
<style scoped>
.modal {
  z-index: 999;
  background-color: rgba(0, 0, 0, 0.3);
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}
.modal-content {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  background-color: white;
  min-height: 40px;
}

.list {
  padding: 2px 10px;
}
.item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 48px;
}
.item + .item {
  border-top: 1px solid #eee;
}

.item-coin {
  display: flex;
  align-items: center;
}
.coin-icon {
  margin-right: 4px;
}
.coin-icon img {
  display: inline-block;
  width: 30px;
  height: 30px;
  border-radius: 30px;
  outline: none;
}
.coin-name {
  font-size: 16px;
}

.item-selector {
  font-size: 16px;
  color: orange;
}
</style>
